﻿Imports VB = Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Imports tessnet2, System.Math, System.Threading.Thread
Public Class FrmMain
    Public Enum TareasEnum
        Cosechar = 1
        Recoger = 2
        ComprarPaja = 3
        venderPaja = 4
        ComprarVenderPaja = 5
    End Enum


    Public Structure Angel
        Dim Edad As Integer
        Dim Nombre As String
    End Structure

    Public Structure TareaStruct
        Dim Area As AreaStructure
        Dim Tarea As TareasEnum
    End Structure

    Public Structure AreaStructure
        Dim Home As Point
        Dim Size As Size
    End Structure

    Dim Tareas As New List(Of TareaStruct)
    Dim p1 As Point
    Dim p2 As Point
    Dim p3 As Point
    Dim tamaño As New Size
    Dim pHome As Point
    Dim Hooked As Boolean
    Dim Running As Boolean
    Dim AddAreaStep As Integer = 0
    Private WithEvents kbHook As KeyboardHook
    Private WithEvents HookMouse As MouseHook

    Private Sub kbHook_KeyUp(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyUp
        Me.Text = Key

        Select Case Key
            Case Keys.Q
                puntos.Text = "p1(" & Cursor.Position.X & " , " & Cursor.Position.Y & ")"
                p1 = Cursor.Position
            Case Keys.W
                puntos.Text &= vbCrLf & "p2(" & Cursor.Position.X & " , " & Cursor.Position.Y & ")"
                p2 = Cursor.Position
                tamaño.Width = p2.X - p1.X
                tamaño.Height = p2.Y - p1.Y
                puntos.Text &= vbCrLf & "Width: " & tamaño.Width & " Height: " & tamaño.Height
                Dim asdf As Double
                asdf = Distancia(p1, p2)
                puntos.Text &= vbCrLf & "s = " & asdf & " X:" & Fix(asdf / 25)

            Case Keys.E
                puntos.Text &= vbCrLf & "p3(" & Cursor.Position.X & " , " & Cursor.Position.Y & ")"
                p3 = Cursor.Position
                tamaño.Width = p3.X - p2.X
                tamaño.Height = p3.Y - p2.Y
                Dim asdf As Double
                asdf = Distancia(p2, p3)
                puntos.Text &= vbCrLf & "Width: " & tamaño.Width & " Height: " & tamaño.Height
                puntos.Text &= vbCrLf & "size = " & asdf & " Y:" & Fix(asdf / 25)
            Case Keys.R
                Select Case AddAreaStep
                    Case 0
                        p1 = Cursor.Position
                        AddAreaStep += 1
                        Exit Select
                    Case 1
                        p2 = Cursor.Position
                        AddAreaStep += 1
                        Exit Select
                    Case 2
                        p3 = Cursor.Position
                        Dim A As New AreaStructure
                        A.Home = p1
                        pHome = A.Home
                        A.Size.Width = Fix(Distancia(p1, p2) / 25)
                        A.Size.Height = Fix(Distancia(p2, p3) / 25)

                        txtHomePosX.Text = p1.X
                        txtHomePosY.Text = p1.Y

                        txtfilas.Text = A.Size.Width
                        txtColumnas.Text = A.Size.Height

                        AddAreaStep += 1
                        Exit Select
                    Case 3
                        p1 = Nothing
                        p2 = Nothing
                        p3 = Nothing
                        AddAreaStep = 0
                        Exit Select
                End Select
                lblR.Text = (AddAreaStep)
            Case Keys.D1
                comboTipoTarea.SelectedIndex = 0
            Case Keys.D2
                comboTipoTarea.SelectedIndex = 1
            Case Keys.D3
                comboTipoTarea.SelectedIndex = 2
            Case Keys.D4
                comboTipoTarea.SelectedIndex = 3
            Case Keys.D5
                comboTipoTarea.SelectedIndex = 4
            Case Keys.D6
                comboTipoTarea.SelectedIndex = 5
            Case Keys.A
                Dim NewJob As New TareaStruct
                If txtHomePosX.Text = "" Or txtHomePosY.Text = "" Or _
                txtfilas.Text = "" Or txtfilas.Text = "" Or _
                comboTipoTarea.SelectedIndex < 0 Then
                    'Ni mayes 
                    Me.Text = "ni mayes"
                    Exit Select
                End If
                NewJob.Area.Home.X = txtHomePosX.Text
                NewJob.Area.Home.Y = txtHomePosY.Text
                NewJob.Area.Size.Width = txtfilas.Text
                NewJob.Area.Size.Height = txtColumnas.Text
                NewJob.Tarea = Me.comboTipoTarea.SelectedIndex + 1

                Tareas.Add(NewJob)
                ActualizarLstDoJobs()
            Case Keys.Y
                For Each job As TareaStruct In Tareas
                    Clickeas(job)
                    Sleep(500)
                Next
            Case Settings.HotKeyStartFarm
                'MsgBox("Start Home")

            Case Settings.HotKeySetHome
                'MsgBox("Set Home")
                txtHomePosX.Text = Cursor.Position.X
                txtHomePosY.Text = Cursor.Position.Y
                pHome.X = Cursor.Position.X
                pHome.Y = Cursor.Position.Y
            Case Keys.X
                Application.DoEvents()
                Clickea()
                'VenderPajasxD()
            Case Keys.Z
                CompraVende()
            Case Keys.C
                ComprarPajasxD()
            Case Keys.P
                Running = False
        End Select
    End Sub

    Sub Agregartarea(ByVal Area_ As AreaStructure, ByVal Tarea_ As TareasEnum)

        Dim NuevaTarea As New TareaStruct

        NuevaTarea.Area = Area_
        NuevaTarea.Tarea = Tarea_

        Tareas.Add(NuevaTarea)

        'actualizar(lstbox)
    End Sub

    Sub ActualizarLstDoJobs()
        LstDoJobs.Items.Clear()
        For Each Job As TareaStruct In Tareas
            Dim Str As String
            Str = Job.Tarea
            Select Case Job.Tarea
                Case 1
                    Str = "Cosechar"
                Case 2
                    Str = "Recoger"
                Case 3
                    Str = "ComprarPaja"
                Case 4
                    Str = "venderPaja"
                Case 5
                    Str = "ComprarVenderPaja"
            End Select
            Str &= " Home(" & Job.Area.Home.X & ", " & Job.Area.Home.Y & ")"
            Str &= " Size(" & Job.Area.Size.Width & ", " & Job.Area.Size.Height & ")"

            LstDoJobs.Items.Add(Str)

        Next
    End Sub

    Sub Clickeas(ByVal Job As TareaStruct)
        Dim columnas As Integer
        Dim filas As Integer
        Running = True
        columnas = Job.Area.Size.Height
        filas = Job.Area.Size.Width

        Dim Posx As Integer, PosHomex As Integer
        Dim Posy As Integer, PosHomey As Integer
        Dim TileOffsetx As Integer
        Dim Tileoffsety As Integer
        Dim MenuOkOffSetX As Integer
        Dim MenuOkOffSetY As Integer
        Dim MenuOffSetX As Integer
        Dim MenuOffSetY As Integer

        MenuOkOffSetX = -41
        MenuOkOffSetY = 230

        MenuOffSetX = 10
        MenuOffSetY = 25

        TileOffsetx = 12
        Tileoffsety = 25

        PosHomex = Job.Area.Home.X
        PosHomey = Job.Area.Home.Y

        Posx = PosHomex
        Posy = PosHomex

        Application.DoEvents()

        For i As Integer = 1 To columnas
            If Running = False Then Exit Sub
            For j As Integer = 1 To filas
                Application.DoEvents()
                Clic(Posx, Posy)
                If Running = False Then Exit Sub
                If 1 Or (i Mod 2 = 1) Then
                    Posx += TileOffsetx
                    Posy -= Tileoffsety
                Else
                    Posx -= TileOffsetx
                    Posy += Tileoffsety
                End If
                System.Threading.Thread.Sleep(150)
            Next
            If Running = False Then Exit Sub
            If 1 Or (i Mod 2 = 0) Then
                Posx = PosHomex + (i * TileOffsetx)
                Posy = PosHomey + (i * Tileoffsety)
            Else
                Posx += TileOffsetx
                Posy += Tileoffsety
            End If
        Next
    End Sub
    Private Sub HotKeyHome_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles _
        txtHotKeyHome.KeyDown, txtHotKeyStart.KeyDown
        Dim Caja As TextBox
        Caja = CType(sender, TextBox)
        Caja.Text = IIf(e.Control, "Ctrl + ", "") & IIf(e.Alt, "Alt + ", "") & IIf(e.Shift, "Shift + ", "") & (e.KeyCode.ToString)
        If Caja.Name = "txtHotKeyHome" Then
            Settings.HotKeySetHome = e.KeyCode
        Else
            Settings.HotKeyStartFarm = e.KeyCode
        End If
    End Sub

    Private Sub CancelarTeclas_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles _
        txtHotKeyHome.KeyPress, txtHotKeyStart.KeyPress

        'cancelamos las pulsaciones de teclas en HotKeyHome y HotKeyStart
        e.Handled = True
    End Sub

    Private Sub btnHook_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHook.Click
        If btnHook.Text = "Hook" Then
            btnHook.Text = "UnHook"
            kbHook = New KeyboardHook
        Else
            btnHook.Text = "Hook"
            kbHook = Nothing
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Settings.HotKeySetHome = Keys.H
        Settings.HotKeyStartFarm = Keys.S
        Settings.Delay = 300
        txtHotKeyHome.Text = HotKeySetHome.ToString
        txtHotKeyStart.Text = HotKeyStartFarm.ToString

        comboTipoTarea.Items.Add("Cosechar")
        comboTipoTarea.Items.Add("Recoger")
        comboTipoTarea.Items.Add("ComprarPaja")
        comboTipoTarea.Items.Add("venderPaja")
        comboTipoTarea.Items.Add("ComprarVenderPaja")

    End Sub

    Function Distancia(ByVal p1 As Point, ByVal p2 As Point) As Double
        Dim r1 As Double
        Dim r2 As Double
        r1 = (p2.X - p1.X)
        r2 = p1.Y - p2.Y
        Distancia = Sqrt(r1 * r1 + r2 * r2)
    End Function

    Sub ComprarPajasxD()
        Dim columnas As Integer
        Dim filas As Integer
        Running = True
        columnas = txtColumnas.Text
        filas = txtfilas.Text

        Dim Posx As Integer, PosHomex As Integer
        Dim Posy As Integer, PosHomey As Integer
        Dim TileOffsetx As Integer
        Dim Tileoffsety As Integer
        Dim MenuOkOffSetX As Integer
        Dim MenuOkOffSetY As Integer
        Dim MenuOffSetX As Integer
        Dim MenuOffSetY As Integer

        MenuOkOffSetX = -41
        MenuOkOffSetY = 230

        MenuOffSetX = 10
        MenuOffSetY = 25

        TileOffsetx = txtOffsetTileX.Text
        Tileoffsety = txtOffsetTileY.Text

        PosHomex = pHome.X
        PosHomey = pHome.Y

        Posx = pHome.X
        Posy = pHome.Y


        Clic(853, 811)
        Sleep(500)
        Clic(616, 363)
        Sleep(500)
        Clic(614, 791)
        Sleep(300)

        Application.DoEvents()
        For i As Integer = 1 To columnas
            For j As Integer = 1 To filas
                If Running = False Then Exit Sub
                Clic(Posx, Posy)
                Application.DoEvents()
                If 1 Or (i Mod 2 = 1) Then
                    Posx += TileOffsetx
                    Posy -= Tileoffsety
                Else
                    MsgBox(2)
                    Posx -= TileOffsetx
                    Posy += Tileoffsety
                End If
                System.Threading.Thread.Sleep(Delay)
            Next
            If Running = False Then Exit Sub
            If 1 Or (i Mod 2 = 0) Then
                Posx = PosHomex + (i * TileOffsetx)
                Posy = PosHomey + (i * Tileoffsety)
            Else
                Posx += TileOffsetx
                Posy += Tileoffsety
            End If
        Next
    End Sub


    Sub VenderPajasxD()
        Dim columnas As Integer
        Dim filas As Integer

        columnas = txtColumnas.Text
        filas = txtfilas.Text

        Dim Posx As Integer, PosHomex As Integer
        Dim Posy As Integer, PosHomey As Integer
        Dim TileOffsetx As Integer
        Dim Tileoffsety As Integer
        Dim MenuOkOffSetX As Integer
        Dim MenuOkOffSetY As Integer
        Dim MenuOffSetX As Integer
        Dim MenuOffSetY As Integer

        MenuOkOffSetX = -41
        MenuOkOffSetY = 230

        MenuOffSetX = 10
        MenuOffSetY = 25

        TileOffsetx = txtOffsetTileX.Text
        Tileoffsety = txtOffsetTileY.Text

        PosHomex = pHome.X
        PosHomey = pHome.Y

        Posx = pHome.X
        Posy = pHome.Y

        Clic(802, 758)
        Sleep(200)
        Clic(806, 693)
        Sleep(300)

        For i As Integer = 1 To columnas
            If Running = False Then Exit Sub
            For j As Integer = 1 To filas
                If Running = False Then Exit Sub
                Clic(Posx, Posy)
                Sleep(100)
                Clic(Posx + MenuOffSetX, Posy + MenuOffSetY)
                Sleep(150)
                Clic(483, 636)

                If 1 Or (i Mod 2 = 1) Then
                    Posx += TileOffsetx
                    Posy -= Tileoffsety
                Else
                    Posx -= TileOffsetx
                    Posy += Tileoffsety
                End If
                System.Threading.Thread.Sleep(200)
            Next

            If 1 Or (i Mod 2 = 0) Then
                Posx = PosHomex + (i * TileOffsetx)
                Posy = PosHomey + (i * Tileoffsety)
            Else
                Posx += TileOffsetx
                Posy += Tileoffsety
            End If
        Next
    End Sub
    Sub Clickea()
        Dim columnas As Integer
        Dim filas As Integer
        Running = True
        columnas = txtColumnas.Text
        filas = txtfilas.Text

        Dim Posx As Integer, PosHomex As Integer
        Dim Posy As Integer, PosHomey As Integer
        Dim TileOffsetx As Integer
        Dim Tileoffsety As Integer
        Dim MenuOkOffSetX As Integer
        Dim MenuOkOffSetY As Integer
        Dim MenuOffSetX As Integer
        Dim MenuOffSetY As Integer

        MenuOkOffSetX = -41
        MenuOkOffSetY = 230

        MenuOffSetX = 10
        MenuOffSetY = 25

        TileOffsetx = txtOffsetTileX.Text
        Tileoffsety = txtOffsetTileY.Text

        PosHomex = pHome.X
        PosHomey = pHome.Y

        Posx = pHome.X
        Posy = pHome.Y

        Application.DoEvents()

        For i As Integer = 1 To columnas
            If Running = False Then Exit Sub
            For j As Integer = 1 To filas
                Application.DoEvents()
                Clic(Posx, Posy)
                If Running = False Then Exit Sub
                If 1 Or (i Mod 2 = 1) Then
                    Posx += TileOffsetx
                    Posy -= Tileoffsety
                Else
                    Posx -= TileOffsetx
                    Posy += Tileoffsety
                End If
                System.Threading.Thread.Sleep(100)
            Next
            If Running = False Then Exit Sub
            If 1 Or (i Mod 2 = 0) Then
                Posx = PosHomex + (i * TileOffsetx)
                Posy = PosHomey + (i * Tileoffsety)
            Else
                Posx += TileOffsetx
                Posy += Tileoffsety
            End If
        Next
    End Sub
    Private Sub txtColumnas_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Char.IsLetter(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

    Sub CompraVende()
        For i As Integer = 1 To CInt(txtCiclos.Text)
            Application.DoEvents()
            ComprarPajasxD()
            Sleep(500)
            VenderPajasxD()
        Next
    End Sub

    Private Sub cursorlbl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cursorlbl.Click

        Clic(853, 811)
        Sleep(500)
        Clic(616, 390)
        Sleep(500)
        Clic(614, 778)
        Sleep(200)
    End Sub

    Private Sub btnAddTareita_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddTareita.Click
        Dim NewJob As New TareaStruct
        NewJob.Area.Home.X = txtHomePosX.Text
        NewJob.Area.Home.Y = txtHomePosX.Text
        NewJob.Area.Size.Width = txtfilas.Text
        NewJob.Area.Size.Height = txtColumnas.Text
        NewJob.Tarea = Me.LstDoJobs.SelectedIndex + 1

        Tareas.Add(NewJob)
        ActualizarLstDoJobs()
    End Sub
End Class


